package com.example.lc.myandroiddemo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends Activity {
    private final int title_image_size = 50;// 左上角图标大小
    private final int title_hospital_size = 30;// 左上角XXXX医院文字大小
    private final int title_centre_size = 23;// 头部中间文字大小
    private final int text_size = 12;// 正文文字大小
    private final int text_image_size_width = 146;// 内容图片大小宽
    private final int text_image_size_height = 121;// 内容图片大小高

    private String title_hospital_content = "北京协和医院";// 左上角XXXX医院文字
    private String title_centre_content = "肛肠镜检查报告单";// 头部中间文字
    private String examine_number = "201610211004";// 检查号
    private String report_time = "2017/02/27";// 报告时间
    private String name = "黄海";// 姓名
    private String sex = "男";// 性别
    private String age = "11";// 年龄
    private String examine_type;// 科别
    private String outpatient_number;// 门诊号
    private String in_hospital_number;// 住院号
    private String bed_number;// 床号
    private String part;// 部位
    private String clinic_diagnose;// 临床诊断
    private String diagnosis_doctor;// 送诊医师
    private String examine_doctor;// 检查医师

    String filePath;

    // 高度
    private final int a = 710;// 检查号 报告时间
    private final int b = 677;// 姓名 性别 年龄 科别
    private final int c = 654;// 门诊号 住院号 床号
    private final int d = 630;// 部位  临床诊断  送诊医师
    // 宽度
    private final int aa = 73;// 姓名  门诊号  部位
    private final int bb = 213;// 性别 住院号 临床诊断
    private final int cc = 420;// 科别 床号 送诊医师

    private final int imaget_height_one_line = 476;// 第一行图片高度
    private final int imaget_height_two_line = 339;// 第二行图片高度

    private int image_all_number = 6;// 报告图片总数量


    // 需要根据图片数量动态改变的高度值
    // 第三条横线高度
    private int three_line_height = 0;
    // 检查所见高度
    private int examine__height = 0;
    // 诊断高度
    private int diagnose__height = 0;
    // 检查所见里面的内容上边高度
    private int examine__content_height_up = 0;
    // 检查所见里面的内容下边高度
    private int examine__content_height_down = 0;
    // 诊断里面的内容上边高度
    private int diagnose__content_height_up = 0;
    // 诊断里面的内容下边高度
    private int diagnose__content_height_down = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            // 英字
//            Font blueFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL, new CMYKColor(255, 0, 0, 0));
//            Font redFont = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD, new CMYKColor(0, 255, 0, 0));
//            Font yellowFont = FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 0, 255, 0));
            // 中字
            String yaHeiFontName = getResources().getString(R.raw.simsun);// 宋体
            yaHeiFontName += ",1";
            BaseFont china_base_font = BaseFont.createFont(yaHeiFontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//中文
            Font china_font = new Font(china_base_font);//中文
            // 字体颜色
            BaseColor buleColor = new BaseColor(35, 106, 192);// 蓝色

            //页面大小
            Rectangle rect = new Rectangle(PageSize.A4);// 现实210mm X 297mm  代码(595.0F, 842.0F)
            //页面背景色
            // rect.setBackgroundColor(BaseColor.ORANGE);// 橙色
            // 创建A4大小PDF文件
            Document document = new Document(rect);
            // 输出位置
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/sdcard/doc.pdf"));
            //页边空白
            document.setMargins(54, 49, 0, 0);
            document.open();

            // 左上角图标
            titleImage(document);

            PdfContentByte cbb = writer.getDirectContent();// 高级布局

            // 医院名称
            cbb.beginText();
            cbb.setFontAndSize(china_base_font, title_hospital_size);
            cbb.setColorFill(buleColor);// 蓝色
            cbb.setTextMatrix(94, 773);
            cbb.showText(title_hospital_content);
            cbb.endText();
            // 肛肠镜检查报告单
            cbb.beginText();
            cbb.setFontAndSize(china_base_font, title_centre_size);
            cbb.setColorFill(buleColor);
            cbb.setTextMatrix(222, 735);
            cbb.showText(title_centre_content);
            cbb.endText();

            // 第一行内容
            oneLineContent(china_base_font, cbb);

            // 第一条分割线
            LineSeparator lineSeparator = new LineSeparator();
            lineSeparator.drawLine(cbb, 56, 547, 700);

            // 第二行内容
            towLineContent(cbb);

            // 第三行内容
            threeLineContent(cbb);

            // 第四行内容
            fourLineContent(cbb);

            // 第二条分割线
            lineSeparator.drawLine(cbb, 56, 547, 613);

            // 六张图片预览
            imageListShow(document);

            // 第三条分割线
            lineSeparator.drawLine(cbb, 56, 547, three_line_height);

            // 第四条分割线
            lineSeparator.drawLine(cbb, 56, 547, 56);


            // 检查所见
            cbb.beginText();
            cbb.setTextMatrix(60, examine__height);
            cbb.showText("检查所见:");
            cbb.endText();
            // 诊断
            cbb.beginText();
            cbb.setTextMatrix(60, diagnose__height);
            cbb.showText("诊断:");
            cbb.endText();


            // 检查所见里面的内容
            ColumnText ct = new ColumnText(cbb);
            ct.setSimpleColumn(60, examine__content_height_up, 537, examine__content_height_down);
            Paragraph p = new Paragraph();
            List list = new List();
            list.add(new ListItem("1111\n" +
                    "111省道1111\n" +
                    "111发11\n" +
                    "11说说 \n" +
                    "222222\n" +
                    "a\n" +
                    "b\n" +
                    "c\n" +
                    "b那个滑头小伙子，为何不和我说话？\n都是因你的缘故，使我饭也吃不下。那个滑头小伙子，为何不和我说话？\n" +
                    "都是因你的缘故，使我饭也吃不下。那个滑头小伙子，为何不和我说话？\n" +
                    "都是因你的缘故，使我饭也吃不下。", china_font));
            p.add(list);
            ct.addElement(p);
            ct.go();

            // 诊断里面的内容
            ColumnText cttwo = new ColumnText(cbb);
            cttwo.setSimpleColumn(60, diagnose__content_height_up, 537, diagnose__content_height_down);
            Paragraph p22 = new Paragraph();
            List list22 = new List();
            list22.add(new ListItem("1111\n111省道1111\n111发11\n11说说 \n222222\na\nb\nc\nb1111\n" +
                    "111省道1111\n" +
                    "111发11\n" +
                    "11说说 \n" +
                    "222222\n" +
                    "a\n" +
                    "b\n" +
                    "c\n" +
                    "b", china_font));
            p22.add(list22);
            cttwo.addElement(p22);
            cttwo.go();


            // 本报告仅供临床参考
            cbb.beginText();
            cbb.setTextMatrix(60, 27);
            cbb.showText("本报告仅供临床参考");
            cbb.endText();
            // 检查医师
            cbb.beginText();
            cbb.setTextMatrix(412, 27);
            cbb.showText("检查医师: " + examine_doctor);
            cbb.endText();

            document.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void imageListShow(Document document) throws IOException, DocumentException {
        if (image_all_number <= 3) {
            three_line_height = 460;
            examine__height = 434;
            diagnose__height = 220;

            examine__content_height_up = 427;
            examine__content_height_down = 240;

            diagnose__content_height_up = 212;
            diagnose__content_height_down = 65;
        } else {
            three_line_height = 321;
            examine__height = 295;
            diagnose__height = 191;

            examine__content_height_up = 288;
            examine__content_height_down = 210;

            diagnose__content_height_up = 183;
            diagnose__content_height_down = 65;
        }
        if (image_all_number == 1) {
            // 图片一
            Image image1 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image1.setAbsolutePosition(225, imaget_height_one_line); // 固定位置
            image1.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image1);
        } else if (image_all_number == 2) {
            // 图片一
            Image image1 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image1.setAbsolutePosition(144, imaget_height_one_line); // 固定位置
            image1.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image1);
            // 图片二
            Image image2 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image2.setAbsolutePosition(306, imaget_height_one_line); // 固定位置
            image2.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image2);
        } else if (image_all_number == 3) {
            // 图片一
            Image image1 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image1.setAbsolutePosition(63, imaget_height_one_line); // 固定位置
            image1.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image1);
            // 图片二
            Image image2 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image2.setAbsolutePosition(229, imaget_height_one_line); // 固定位置
            image2.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image2);
            // 图片三
            Image image3 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image3.setAbsolutePosition(392, imaget_height_one_line); // 固定位置
            image3.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image3);
        } else if (image_all_number == 4) {
            // 图片一
            Image image1 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image1.setAbsolutePosition(144, imaget_height_one_line); // 固定位置
            image1.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image1);
            // 图片二
            Image image2 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image2.setAbsolutePosition(306, imaget_height_one_line); // 固定位置
            image2.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image2);
            // 图片三
            Image image3 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image3.setAbsolutePosition(144, imaget_height_two_line); // 固定位置
            image3.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image3);
            // 图片四
            Image image4 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image4.setAbsolutePosition(306, imaget_height_two_line); // 固定位置
            image4.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image4);
        } else if (image_all_number == 5) {
            // 图片一
            Image image1 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image1.setAbsolutePosition(63, imaget_height_one_line); // 固定位置
            image1.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image1);
            // 图片二
            Image image2 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image2.setAbsolutePosition(229, imaget_height_one_line); // 固定位置
            image2.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image2);
            // 图片三
            Image image3 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image3.setAbsolutePosition(392, imaget_height_one_line); // 固定位置
            image3.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image3);
            // 图片四
            Image image4 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image4.setAbsolutePosition(63, imaget_height_two_line); // 固定位置
            image4.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image4);
            // 图片五
            Image image5 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image5.setAbsolutePosition(229, imaget_height_two_line); // 固定位置
            image5.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image5);
        } else if (image_all_number == 6) {
            // 图片一
            Image image1 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image1.setAbsolutePosition(63, imaget_height_one_line); // 固定位置
            image1.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image1);
            // 图片二
            Image image2 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image2.setAbsolutePosition(229, imaget_height_one_line); // 固定位置
            image2.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image2);
            // 图片三
            Image image3 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image3.setAbsolutePosition(392, imaget_height_one_line); // 固定位置
            image3.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image3);
            // 图片四
            Image image4 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image4.setAbsolutePosition(63, imaget_height_two_line); // 固定位置
            image4.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image4);
            // 图片五
            Image image5 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image5.setAbsolutePosition(229, imaget_height_two_line); // 固定位置
            image5.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image5);
            // 图片六
            Image image6 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/yljx/lc/off_20170222_2245/picture/20170222012029.jpg");
            image6.setAbsolutePosition(392, imaget_height_two_line); // 固定位置
            image6.scaleAbsolute(text_image_size_width, text_image_size_height); // 设置大小
            document.add(image6);
        }
    }

    private void fourLineContent(PdfContentByte cbb) {
        // 部位
        cbb.beginText();
        cbb.setTextMatrix(aa, d);
        cbb.showText("部  位: " + part);
        cbb.endText();
        // 临床诊断
        cbb.beginText();
        cbb.setTextMatrix(bb, d);
        cbb.showText("临床诊断: " + clinic_diagnose);
        cbb.endText();
        // 送诊医师
        cbb.beginText();
        cbb.setTextMatrix(cc, d);
        cbb.showText("送诊医师: " + diagnosis_doctor);
        cbb.endText();
    }

    private void threeLineContent(PdfContentByte cbb) {
        // 门诊号
        cbb.beginText();
        cbb.setTextMatrix(aa, c);
        cbb.showText("门诊号: " + outpatient_number);
        cbb.endText();
        // 住院号
        cbb.beginText();
        cbb.setTextMatrix(bb, c);
        cbb.showText("住 院 号: " + in_hospital_number);
        cbb.endText();
        // 床号
        cbb.beginText();
        cbb.setTextMatrix(cc, c);
        cbb.showText("床    号: " + bed_number);
        cbb.endText();
    }

    private void towLineContent(PdfContentByte cbb) {
        // 姓名
        cbb.beginText();
        cbb.setTextMatrix(aa, b);
        cbb.showText("姓  名: " + name);
        cbb.endText();
        // 性别
        cbb.beginText();
        cbb.setTextMatrix(bb, b);
        cbb.showText("性    别: " + sex);
        cbb.endText();
        // 年龄
        cbb.beginText();
        cbb.setTextMatrix(323, b);
        cbb.showText("年龄: " + age);
        cbb.endText();
        // 科别
        cbb.beginText();
        cbb.setTextMatrix(cc, b);
        cbb.showText("科    别: " + examine_type);
        cbb.endText();
    }

    private void oneLineContent(BaseFont china_base_font, PdfContentByte cbb) {
        // 检查号
        cbb.beginText();
        cbb.setFontAndSize(china_base_font, text_size);
        cbb.setColorFill(BaseColor.BLACK);// 黑色
        cbb.setTextMatrix(59, a);
        cbb.showText("检查号: " + examine_number);
        cbb.endText();
        // 报告时间
        cbb.beginText();
        cbb.setTextMatrix(413, a);
        cbb.showText("报告时间: " + report_time);
        cbb.endText();
    }


    /**
     * 左上角图标
     *
     * @param document
     * @throws IOException
     * @throws DocumentException
     */
    private void titleImage(Document document) throws IOException, DocumentException {
        // 图片 Add Image
        InputStream open = getAssets().open("case_icon.jpg");
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = open.read(buffer))) {
            output.write(buffer, 0, n);
        }
        Image image1 = Image.getInstance(output.toByteArray());
//            Image image1 = Image.getInstance(Environment.getExternalStorageDirectory().toString() + "/case_icon.jpg");
        // 固定位置
        image1.setAbsolutePosition(41, 760);
        // 设置大小
        image1.scaleAbsolute(title_image_size, title_image_size);
        document.add(image1);
    }

    public void ceshi(View view) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/sdcard/doc.pdf")));
//        shareIntent.setType("image/jpeg");
        shareIntent.setType("application/pdf");
        startActivity(Intent.createChooser(shareIntent, "分享"));
    }
}
